package com.android.lovingpet.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;

/**
 * Created by ikata on 2018/3/4.
 */

public abstract class BaseActivity <V extends BaseView,P extends BasePresenter<V>> extends AppCompatActivity implements BaseView {

    private View progress;
    private V view;
    private P presenter;

    public P getPresenter() {
        return presenter;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.presenter==null)
        {
            this.presenter=createPresenter();
        }
        if(this.view==null)
        {
            this.view=createView();
        }
        if(this.view!=null&&this.presenter!=null)
        {
            this.presenter.attachView(this.view);
        }

    }

    public void setTitle(String title)
    {
        RelativeLayout relativeLayout=findViewById(R.id.rl_title);
        if (relativeLayout!=null)
        {
            Log.d("ssafewfs","DAJFLJWEOF");
            TextView textView=relativeLayout.findViewById(R.id.tv_title);
            textView.setText(title);
        }
    }


    public void reBind() {
        this.presenter=createPresenter();
        this.view=createView();
        this.presenter.attachView(this.view);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initProgress();
    }

    public abstract P createPresenter();
    public abstract V createView();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(this.presenter!=null&&this.view!=null)
        {
            this.presenter.detachView();
        }
    }

    @Override
    public void showToast(final String meg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this, meg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progress != null) {
                    progress.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void hideProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progress != null) {
                    progress.setVisibility(View.GONE);
                }
            }
        });
    }

    public static void add(FragmentManager fm, @IdRes int containerViewId, Fragment fragment) {
        fm
                .beginTransaction()
                .add(containerViewId, fragment)
                .addToBackStack(null)
                .commit();
    }

    public static void replaceWithAnim(FragmentManager fm, @IdRes int containerViewId, Fragment fragment) {
        fm
                .beginTransaction()
                .setCustomAnimations(
                        R.anim.slide_right_in,
                        R.anim.slide_left_out,
                        R.anim.slide_left_in,
                        R.anim.slide_right_out)
                .replace(containerViewId, fragment)
                .addToBackStack(null)
                .commit();
    }

    private void initProgress() {
        View view = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        if (view == null || !(view instanceof ViewGroup)) {
            return;
        }
        ViewGroup viewGroup = (ViewGroup) view;
        progress = LayoutInflater.from(this).inflate(R.layout.loading, viewGroup, false);
        if (progress != null) {
            viewGroup.addView(progress);
            progress.bringToFront();
            progress.setOnClickListener(null);
        }
        View back = view.findViewById(R.id.bt_back);
        if (back != null) {
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

}
